﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="kapitaliJagunemineType_v3", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(KapitaliJagunemineTypeV3))]
    public class KapitaliJagunemineTypeV3Serializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IKapitaliJagunemineTypeV3 clazz;
        
        public KapitaliJagunemineTypeV3Serializer(IKapitaliJagunemineTypeV3 clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("id");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "integer");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdInteger(((decimal)(clazz.Id))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("tegevus");
            if (clazz.Tegevus!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Tegevus))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Tegevus");
            }
            Writer.WriteEndElement();
            if (clazz.Omand!=null) {
                Writer.WriteStartElement("omand");
                global::Xtee.Core.SerializationMetadata metaOmand = this.GetMetaData(clazz.Omand);
                Writer.WriteTypeAttribute(metaOmand.TypeQualifiedName.Namespace, metaOmand.TypeQualifiedName.Name);
                metaOmand.NewSerializer(clazz.Omand).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            for (int x = 0; ((clazz.Osapandid != null) 
                        && (x < clazz.Osapandid.Count)); x = (x + 1)) {
                if (clazz.Osapandid[x]!=null) {
                    Writer.WriteStartElement("osapandid");
                    global::Xtee.Core.SerializationMetadata metaOsapandid = this.GetMetaData(clazz.Osapandid[x]);
                    Writer.WriteTypeAttribute(metaOsapandid.TypeQualifiedName.Namespace, metaOsapandid.TypeQualifiedName.Name);
                    metaOsapandid.NewSerializer(clazz.Osapandid[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (clazz.TingimuslikYleminek!=null) {
                Writer.WriteStartElement("tingimuslik_yleminek");
                global::Xtee.Core.SerializationMetadata metaTingimuslikYleminek = this.GetMetaData(clazz.TingimuslikYleminek);
                Writer.WriteTypeAttribute(metaTingimuslikYleminek.TypeQualifiedName.Namespace, metaTingimuslikYleminek.TypeQualifiedName.Name);
                metaTingimuslikYleminek.NewSerializer(clazz.TingimuslikYleminek).WriteXml(Writer);
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType_v3>id");
                }
                clazz.Id = new global::Xtee.Core.Schema.XsdInteger(Reader.ReadElementString("id")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>kapitaliJagunemineType_v3>id", Reader.LocalName);
            }
            if (Reader.LocalName=="tegevus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType_v3>tegevus");
                }
                clazz.Tegevus = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("tegevus")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>kapitaliJagunemineType_v3>tegevus", Reader.LocalName);
            }
            if (Reader.LocalName=="omand") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType_v3>omand");
                }
                global::Xtee.Core.SerializationMetadata metaOmand = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOmandTypeV3>(Reader, "omandType_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Omand = metaOmand.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOmandTypeV3>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("omand");
                    metaOmand.NewSerializer(clazz.Omand).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="osapandid") {
                clazz.Osapandid = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOsapantTypeV3>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "osapandid")); ) {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaOsapandid = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOsapantTypeV3>(Reader, "osapantType_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOsapantTypeV3 item = metaOsapandid.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOsapantTypeV3>();
                        clazz.Osapandid.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("osapandid");
                            metaOsapandid.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.Osapandid.Add(null);
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="tingimuslik_yleminek") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType_v3>tingimuslik_yleminek");
                }
                global::Xtee.Core.SerializationMetadata metaTingimuslikYleminek = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKitsendusedTypeV3>(Reader, "kitsendusedType_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.TingimuslikYleminek = metaTingimuslikYleminek.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKitsendusedTypeV3>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("tingimuslik_yleminek");
                    metaTingimuslikYleminek.NewSerializer(clazz.TingimuslikYleminek).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
        }
    }
}
